﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMS.Model;

namespace WMS.Common
{
    using System.Net;

    namespace WMS.Common
    {
        /// <summary>
        /// 统一API返回结果帮助类
        /// </summary>
        public static class ResultHelper
        {
            #region 成功结果

            /// <summary>
            /// 成功返回结果
            /// </summary>
            /// <param name="data">返回数据</param>
            /// <returns></returns>
            public static ResultApi Success(object data)
            {
                return new ResultApi()
                {
                    Code = "200",
                    Data = data,
                    Message = "成功",
                    Success = true
                };
            }

            /// <summary>
            /// 成功返回结果（带自定义消息）
            /// </summary>
            /// <param name="data">返回数据</param>
            /// <param name="message">自定义消息</param>
            /// <returns></returns>
            public static ResultApi Success(object data, string message)
            {
                return new ResultApi()
                {
                    Code = "200",
                    Data = data,
                    Message = message,
                    Success = true
                };
            }

            /// <summary>
            /// 成功返回结果（无数据）
            /// </summary>
            /// <param name="message">自定义消息</param>
            /// <returns></returns>
            public static ResultApi Success(string message = "操作成功")
            {
                return new ResultApi()
                {
                    Code = "200",
                    Data = null,
                    Message = message,
                    Success = true
                };
            }

            /// <summary>
            /// 成功返回分页数据
            /// </summary>
            /// <typeparam name="T">数据类型</typeparam>
            /// <param name="data">数据列表</param>
            /// <param name="total">总记录数</param>
            /// <param name="page">当前页码</param>
            /// <param name="pageSize">每页大小</param>
            /// <returns></returns>
            public static ResultApi Success<T>(List<T> data, long total, int page, int pageSize)
            {
                return new ResultApi()
                {
                    Code = "200",
                    Data = new
                    {
                        List = data,
                        Total = total,
                        Page = page,
                        PageSize = pageSize,
                        PageCount = (int)Math.Ceiling(total / (double)pageSize)
                    },
                    Message = "成功",
                    Success = true
                };
            }

            #endregion

            #region 错误结果

            /// <summary>
            /// 错误返回结果
            /// </summary>
            /// <param name="message">错误消息</param>
            /// <returns></returns>
            public static ResultApi Error(string message)
            {
                return new ResultApi()
                {
                    Code = "500",
                    Data = null,
                    Message = message,
                    Success = false
                };
            }

            /// <summary>
            /// 错误返回结果（带错误码）
            /// </summary>
            /// <param name="code">错误码</param>
            /// <param name="message">错误消息</param>
            /// <returns></returns>
            public static ResultApi Error(string code, string message)
            {
                return new ResultApi()
                {
                    Code = code,
                    Data = null,
                    Message = message,
                    Success = false
                };
            }

            /// <summary>
            /// 错误返回结果（带数据）
            /// </summary>
            /// <param name="message">错误消息</param>
            /// <param name="data">错误数据</param>
            /// <returns></returns>
            public static ResultApi Error(string message, object data)
            {
                return new ResultApi()
                {
                    Code = "500",
                    Data = data,
                    Message = message,
                    Success = false
                };
            }

            #endregion

            #region 特定状态码结果

            /// <summary>
            /// 未授权访问
            /// </summary>
            /// <param name="message">消息</param>
            /// <returns></returns>
            public static ResultApi Unauthorized(string message = "未授权访问")
            {
                return new ResultApi()
                {
                    Code = "401",
                    Data = null,
                    Message = message,
                    Success = false
                };
            }

            /// <summary>
            /// 禁止访问
            /// </summary>
            /// <param name="message">消息</param>
            /// <returns></returns>
            public static ResultApi Forbidden(string message = "禁止访问")
            {
                return new ResultApi()
                {
                    Code = "403",
                    Data = null,
                    Message = message,
                    Success = false
                };
            }

            /// <summary>
            /// 资源未找到
            /// </summary>
            /// <param name="message">消息</param>
            /// <returns></returns>
            public static ResultApi NotFound(string message = "资源未找到")
            {
                return new ResultApi()
                {
                    Code = "404",
                    Data = null,
                    Message = message,
                    Success = false
                };
            }

            /// <summary>
            /// 请求参数错误
            /// </summary>
            /// <param name="message">消息</param>
            /// <returns></returns>
            public static ResultApi BadRequest(string message = "请求参数错误")
            {
                return new ResultApi()
                {
                    Code = "400",
                    Data = null,
                    Message = message,
                    Success = false
                };
            }

            /// <summary>
            /// 服务器内部错误
            /// </summary>
            /// <param name="message">消息</param>
            /// <returns></returns>
            public static ResultApi InternalServerError(string message = "服务器内部错误")
            {
                return new ResultApi()
                {
                    Code = "500",
                    Data = null,
                    Message = message,
                    Success = false
                };
            }

            /// <summary>
            /// 服务不可用
            /// </summary>
            /// <param name="message">消息</param>
            /// <returns></returns>
            public static ResultApi ServiceUnavailable(string message = "服务暂时不可用")
            {
                return new ResultApi()
                {
                    Code = "503",
                    Data = null,
                    Message = message,
                    Success = false
                };
            }

            #endregion

            #region 业务相关结果

            /// <summary>
            /// 数据验证失败
            /// </summary>
            /// <param name="errors">验证错误信息</param>
            /// <returns></returns>
            public static ResultApi ValidationFailed(object errors)
            {
                return new ResultApi()
                {
                    Code = "422",
                    Data = errors,
                    Message = "数据验证失败",
                    Success = false
                };
            }

            /// <summary>
            /// 操作失败
            /// </summary>
            /// <param name="message">消息</param>
            /// <returns></returns>
            public static ResultApi OperationFailed(string message = "操作失败")
            {
                return new ResultApi()
                {
                    Code = "400",
                    Data = null,
                    Message = message,
                    Success = false
                };
            }

            /// <summary>
            /// 数据已存在
            /// </summary>
            /// <param name="message">消息</param>
            /// <returns></returns>
            public static ResultApi DataExists(string message = "数据已存在")
            {
                return new ResultApi()
                {
                    Code = "409",
                    Data = null,
                    Message = message,
                    Success = false
                };
            }

            /// <summary>
            /// 自定义业务错误
            /// </summary>
            /// <param name="code">业务错误码</param>
            /// <param name="message">错误消息</param>
            /// <param name="data">相关数据</param>
            /// <returns></returns>
            public static ResultApi BusinessError(string code, string message, object data = null)
            {
                return new ResultApi()
                {
                    Code = code,
                    Data = data,
                    Message = message,
                    Success = false
                };
            }

            #endregion

            #region 便捷方法

            /// <summary>
            /// 根据布尔值返回结果
            /// </summary>
            /// <param name="success">是否成功</param>
            /// <param name="successMessage">成功消息</param>
            /// <param name="errorMessage">失败消息</param>
            /// <returns></returns>
            public static ResultApi FromBool(bool success, string successMessage = "操作成功", string errorMessage = "操作失败")
            {
                return success ? Success(successMessage) : Error(errorMessage);
            }

            /// <summary>
            /// 根据操作影响行数返回结果
            /// </summary>
            /// <param name="rows">影响行数</param>
            /// <param name="successMessage">成功消息</param>
            /// <param name="errorMessage">失败消息</param>
            /// <returns></returns>
            public static ResultApi FromRows(int rows, string successMessage = "操作成功", string errorMessage = "操作失败")
            {
                return rows > 0 ? Success(successMessage) : Error(errorMessage);
            }

            /// <summary>
            /// 根据对象是否存在返回结果
            /// </summary>
            /// <param name="obj">对象</param>
            /// <param name="successMessage">成功消息</param>
            /// <param name="errorMessage">失败消息</param>
            /// <returns></returns>
            public static ResultApi FromObject(object obj, string successMessage = "查询成功", string errorMessage = "数据不存在")
            {
                return obj != null ? Success(obj, successMessage) : NotFound(errorMessage);
            }

            #endregion
        }

        /// <summary>
        /// 统一API返回模型
        /// </summary>
        public class ResultApi
        {
            /// <summary>
            /// 状态码
            /// </summary>
            public string Code { get; set; }

            /// <summary>
            /// 数据
            /// </summary>
            public object Data { get; set; }

            /// <summary>
            /// 消息
            /// </summary>
            public string Message { get; set; }

            /// <summary>
            /// 是否成功
            /// </summary>
            public bool Success { get; set; }
        }
    }
}
